package org.ttjhome.db.dbo;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
 
@Entity
@Table(name="Candidates")
public class Candidate implements Serializable {

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;
	
	
	@Version
	private int version;
	
	@Column(length=60, nullable=false)
	private String name;
	
	@Column(length=60, nullable=false)
	private String idNumber;
	
	

	@Column(length=10, nullable=false)
	private String gender;
	
	@OneToMany(mappedBy="candidate",cascade = {CascadeType.ALL},fetch = FetchType.EAGER)
	private Set<Resume> resumes = new HashSet<Resume>();

	
	@Column
	private Date birthDay;
	
	@Column
	private Date workFrom;
	
	@ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name="Candidate_Tags",
               joinColumns=@JoinColumn(name="CANDIDATE_ID"),
               inverseJoinColumns=@JoinColumn(name="TAG_ID"))
    private Set<Tag> tags = new HashSet<Tag>();

	@Column
	private String mailAddress ;

	public String getMailAddress() {
		return mailAddress;
	}

	public void setMailAddress(String mailAddress) {
		this.mailAddress = mailAddress;
	}

	public Set<Tag> getTags() {
		return tags;
	}

	public void setTags(Set<Tag> tags) {
		this.tags = tags;
	}

	public Date getWorkFrom() {
		return workFrom;
	}

	public void setWorkFrom(Date workFrom) {
		this.workFrom = workFrom;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Date getBirthDay() {
		return birthDay;
	}

	public void setBirthDay(Date birthDay) {
		this.birthDay = birthDay;
	}


	public String getIdNumber() {
		return idNumber;
	}

	public void setIdNumber(String idNumber) {
		this.idNumber = idNumber;
	}

	public Set<Resume> getResumes() {
		return resumes;
	}

	public void setResumes(Set<Resume> resumes) {
		this.resumes = resumes;
	}

	public int getAge()
	{
		return passedYears(birthDay);
	}
	
	public int getExperiences(){
		return passedYears(workFrom);
	}
	
	private int passedYears(Date passedDate){
		if(passedDate==null)  return -1;
		Calendar now = Calendar.getInstance();
		now.setTime(new Date());
		Calendar passed =Calendar.getInstance();
		passed.setTime(passedDate);
		return now.get(Calendar.YEAR)-passed.get(Calendar.YEAR);
	}

}
